<?php

defined('SYSPATH') or die('No direct script access.');

abstract class Controller_WebInterface extends Controller_Template {

    protected $_title = 'Portal';
    protected $_meta;
    protected $_array_css = array();
    protected $_array_script = array();
    protected $_template;
    protected $_seo_keyword;
    protected $_url_base_theme;
    protected $_url_base_media;
    protected $_url_base_media_location;
    protected $_layout = 'layout';
    protected $_language = 'vn';
    private static $_CST_DEFAULT_FOLDER_THEMES = "themes";
    private static $_CST_DEFAULT_FOLDER_MEDIA = "media";
    private static $_CST_DEFAULT_FOLDER_TEMPLATE = "default";
    protected $_redirect;

    public function setTemplate($template) {

        $this->template = "../template/master";
        $o_config = Kohana::$config->load('template');
        $a_template = $o_config->$template;
        if (!empty($a_template)) {
            foreach ($a_template as $key => $value) {
                $key = "_" . $key;
                if (empty($this->$key)) {
                    $this->$key = $value;
                }
            }
        }

        $this->_template = $template . "/" . $this->_template;

        if (empty($this->_url_base_theme)) {
            $this->_url_base_theme = URL::base() . self::$_CST_DEFAULT_FOLDER_MEDIA . "/" . self::$_CST_DEFAULT_FOLDER_THEMES . '/' . $this->_template;
        } else {
            $this->_url_base_theme = $this->_url_base_theme . "/" . $this->_template;
        }

        if (empty($this->_url_base_media)) {
            $this->_url_base_media = URL::base() . self::$_CST_DEFAULT_FOLDER_MEDIA;
        }
        if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/application/template/" . $this->_template)) {
            $this->_template = $template . "/" . self::$_CST_DEFAULT_FOLDER_TEMPLATE;
        }
        View::bind_global('title', $this->_title);
        View::bind_global('meta', $this->_meta);
        View::bind_global('array_css', $this->_array_css);
        View::bind_global('array_script', $this->_array_script);
        View::bind_global('template', $this->_template);
        View::bind_global('seo_keyword', $this->_seo_keyword);
        View::bind_global('url_base_theme', $this->_url_base_theme);
        View::bind_global('url_base_media', $this->_url_base_media);
        View::bind_global('url_base_media_location', $this->_url_base_media_location);
        View::bind_global('layout', $this->_layout);

        I18n::lang($this->getLanguage());
        View::bind_global('_language', $this->_language);

        $redirect = Arr::get($_GET, 'redirect');
        $this->_redirect = $redirect;
        View::bind_global('redirect', $redirect);
    }

    /**
     * code : View::set_global($name, $value);
     * @param <type> $name
     * @param <type> $value
     */
    public function __set($name, $value) {
        if (property_exists($this, $name)) {
            $this->$name = $value;
        }
    }

    /**
     *
     * @param <type> $name
     * @return <type>
     */
    public function __get($name) {
        if (property_exists($this, $name)) {
            return $this->$name;
        }
        return null;
    }

    /**
     * ex: $this->setTitle("Cls");
     * $this->getTitle("Cls");
     * just to build function setParame, getParame
     * @param <type> $name
     * @param <type> $arguments
     * @return <type>
     */
    public function __call($name, $arguments) {
        $s_action = strtolower(substr($name, 0, 3));
        $s_fun = strtolower(substr($name, 3));
        //for action getParame
        $s_fun = "_" . $s_fun;
        if ($s_action === "get") {
            return $this->$s_fun;
        }
        //for action setParame
        else if ($s_action === "set") {
            $this->$s_fun = $arguments[0];
        }
    }

    /**
     *
     * @param <type> $a_js 
     */
    public function add_js($a_js) {
        $a_js = array_merge($this->_array_script, $a_js);
        $this->setArray_script($a_js);
    }

    /**
     *
     * @param <type> $a_css 
     */
    public function add_css($a_css) {
        $a_css = array_merge($this->_array_css, $a_css);
        $this->setArray_css($a_css);
    }

    /**
     *
     * @param <type> $a_css 
     */
    public function add_css_after($a_css) {
        $temp_css = array();
        if (empty($a_css) || empty($this->_array_css)) {
            return;
        }
        foreach ($this->_array_css as $k => $c) {
            if (is_array($c)) {
                $temp_css[$k] = $c;
            } else {
                $temp_css[] = $c;
            }
            foreach ($a_css as $key => $css) {
                if (is_array($c)) {
                    if($k == $key){
                        $temp_css[] = $css;
                        break;
                    }
                }
                if ($key == $c) {
                    $temp_css[] = $css;
                    break;
                }
            }
        }
        $this->setArray_css($temp_css);
    }

    /**
     *
     * @param <type> $a_css
     */
    public function remove_css($a_css) {
        $a_css = array_diff($this->_array_css, $a_css);
        $this->setArray_css($a_css);
    }

    /**
     *
     * @param <type> $a_css
     */
    public function remove_allcss() {
        $a_css = array();
        $this->setArray_css($a_css);
    }

    /**
     *
     * @param <type> $a_js
     */
    public function remove_js($a_js) {
        $a_js = array_diff($this->_array_script, $a_js);
        $this->setArray_script($a_js);
    }

    /**
     *
     * @param <type> $a_js
     */
    public function remove_alljs() {
        $a_js = array();
        $this->setArray_script($a_js);
    }

    public function get_sort($a_sort = null, $default = null) {
        $sort_by = Arr::get($_GET, 'sort_by');
        $sort_dir = Arr::get($_GET, 'sort_dir');
        $query_sort = array();
        if (!empty($sort_dir) && !empty($sort_dir)) {
            $query_sort = array($sort_by => $sort_dir);
        } else if ($default !== null) {
            $query_sort = array($default => 'asc');
        }
        if ($a_sort != null)
            $a_sort = array_diff($a_sort, $query_sort);
        else
            $a_sort = $query_sort;
        return $a_sort;
    }

    public function get_filter($filter = null, $filter_column = null) {
        $searchtext = Arr::get($_GET, 'searchtext');
        $searchtext = trim($searchtext);
        if ($filter === null && ($filter_column === null || empty($searchtext))) {
            return null;
        }
        if ($filter !== null && ($filter_column === null || empty($searchtext) )) {
            return $filter;
        }
        $_filter = array();
        if ($filter !== null) {
            $_filter = $filter;
        }
        if (!empty($filter)) {
            $_filter["where open"] = "";
        }
        $i = 0;
        foreach ($filter_column as $k => $v) {
            $s_v = $v;
            if ($i > 0) {
                $s_v = "or " . $s_v;
            }
            $_filter[$k] = array($s_v => $searchtext);
            $i++;
        }
        if (!empty($filter)) {
            $_filter["where close"] = "";
        }

        return $_filter;
    }

}

?>
